<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-date-picker type="datetime" placeholder="开始时间" v-model="filters.start_time"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker type="datetime" placeholder="结束时间" v-model="filters.end_time"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-select @change="getNewCode" v-model="filters.role" clearable placeholder="角色类型">
						<el-option
			  				v-for="item,index in roles"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.position" clearable placeholder="角色职位">
						<el-option
			  				v-for="item,index in positions"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.name" placeholder="输入用户名称搜索"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" label="序号" align="center" width="80">
			</el-table-column>
			<el-input prop="id" type="hidden"></el-input>
			<el-table-column prop="fullname" align="center" label="用户名称">
			</el-table-column>
			<el-table-column prop="roleName" align="center" label="角色类型">
			</el-table-column>
			<el-table-column prop="spName" align="center" label="角色职位">
			</el-table-column>
			<el-table-column prop="createDate" align="center" label="创建时间" :formatter="formatDateTime">
			</el-table-column>
			<el-table-column prop="maxLoginTime" align="center" label="最后登录时间" :formatter="formatDateTime">
			</el-table-column>
			<el-table-column label="操作" align="center" width="200">
				<template slot-scope="scope">
					<el-button size="small" @click="handleView(scope.$index, scope.row)">查看</el-button>
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="toolbar" style="display: flex;justify-content: flex-end;">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="pageIndex"
				:page-sizes="[10, 20, 30, 40]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total">
			</el-pagination>
		</div>
		<!--查看界面-->
		<el-dialog title="查看" v-model="viewFormVisible" :close-on-click-modal="false">
			<el-form :model="viewForm" label-width="80px" :rules="viewFormRules" ref="viewForm">
				<el-form-item label="用户名称" prop="fullname">
					<el-input v-model="viewForm.fullname" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="登录账号" prop="username">
					<el-input v-model="viewForm.username" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="登录密码" prop="password">
					<el-input v-model="viewForm.password" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="角色类型" prop="roleName">
					<!-- <el-select v-model="viewForm.role" disabled="disabled" clearable placeholder="角色类型" readonly>
						<el-option
			  				v-for="item in roles"
			  	      			:label="item.name"
			  	      			:value="item.id">
						</el-option>
					</el-select> -->
					<el-input v-model="viewForm.roleName" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="角色职位" prop="spName">
					<!-- <el-select v-model="viewForm.position" disabled="disabled" clearable placeholder="角色职位" readonly>
						<el-option
			  				v-for="item in positions"
			  	      			:label="item.name"
			  	      			:value="item.id">
						</el-option>
					</el-select> -->
					<el-input v-model="viewForm.spName" auto-complete="off" readonly></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="viewFormVisible = false">关闭</el-button>
			</div>
		</el-dialog>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-input v-model="editForm.id" type="hidden"></el-input>
				<el-form-item label="用户名称" prop="fullname">
					<el-input v-model="editForm.fullname" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="登录账号" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<!-- <el-form-item label="登录密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item> -->
				<el-form-item label="角色类型" prop="roleName">
					<el-select @change="getNewCodeEdit" v-model="editForm.roleId" clearable placeholder="角色类型">
						<el-option
			  				v-for="item,index in rolesEdit"
							:key="item.index"
							:label="item.name"
							:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="角色职位" prop="spName">
					<el-select @change="getNewCodeEdit2" v-model="editForm.spName" clearable placeholder="角色职位">
						<el-option
			  				v-for="item,index in positionsEdit"
							:key="item.index"
							:label="item.name"
							:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="用户名称" prop="fullname">
					<el-input v-model="addForm.fullname" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="登录账号" prop="username">
					<el-input v-model="addForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="登录密码" prop="password">
					<el-input v-model="addForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="角色类型" prop="role">
					<el-select @change="getNewCodeAdd" v-model="addForm.role" clearable placeholder="角色类型">
						<el-option
			  				v-for="item,index in rolesAdd"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="角色职位" prop="position">
					<el-select v-model="addForm.position" clearable placeholder="角色职位">
						<el-option
			  				v-for="item,index in positionsAdd"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import moment from '../../common/js/moment'
	import { getRoleList, getPositionList, getSysUserList, editSysUser, addSysUser, removeSysUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					// name: ''
				},
				roles: [],
				positions: [],
				rolesAdd: [],
				positionsAdd: [],
				rolesEdit: [],
				positionsEdit: [],
				users: [],
				total: 0,
				// page: 1,
				pageIndex: 1,
				pageSize: 10,
				listLoading: false,
				sels: [],//列表选中列
				
				viewFormVisible: false,//编辑界面是否显示
				viewLoading: false,
				viewFormRules: {
				},
				//查看界面数据
				viewForm: {
					id: 0,
					fullname: '',
					username: '',
					password: '',
					roleName: '',
					spName: ''
				},

				newPositions: '',
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
				},
				//编辑界面数据
				editForm: {
					id: 0,
					fullname: '',
					username: '',
					roleId: '',
					positionId: ''
				},

				searchRole: '',
				newRole: '',
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					fullname: [
						{ required: true, message: '请输入用户名称', trigger: 'blur' },
					],
					username: [
						{ required: true, message: '请输入登录账号', trigger: 'blur' },
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' },
					]
				},
				//新增界面数据
				addForm: {
					fullname: '',
					username: '',
					password: '',
					role: '',
					position: ''
				}

			}
		},
		methods: {
			formatDateTime: function (row, column){
				var date = row[column.property];
				if (date == undefined) {
		            		return "";
		        	}
				return this.moment(date).format("YYYY-MM-DD HH:mm:ss");
			},
			handleCurrentChange(val) {
				this.pageIndex = val;
				this.getUsers();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
				let auth = localStorage.getItem('authority');
				
				let para = {
					authority: auth,
					pageIndex:this.pageIndex,
					pageSize:this.pageSize,
					fullname: this.filters.name,
					startTime: this.filters.start_time,
					endTime: this.filters.end_time,
					roleId: this.filters.role,
					spId: this.filters.position
				};
				let userIds = JSON.parse( localStorage.getItem('userIds'));
				if(userIds){
					para.userIds = userIds;
				}
				this.listLoading = true;
				getSysUserList(para).then((res) => {
					this.total = res.body.totalCount;
					this.users = res.body.data;
					this.listLoading = false;
				});
			},
			
			//获取角色类型
			getRoles() {
				let auth = localStorage.getItem('authority');
				let role = String(localStorage.getItem('role'));
				let para = {
					authority: auth,
					roleIds: role,
				};
				getRoleList(para).then((res) => {
					this.roles = res.data.data;
				});
			},
			//获取当前下拉框选中值的value代码值
			getNewCode(value) {
				this.newRole = value;
				this.getPositions(this.newRole);
			},
			//获取角色职位列表
			getPositionsAll() {
				let auth = localStorage.getItem('authority');
				let positionIds = JSON.parse( localStorage.getItem('positionIds'));
				let para = {
					authorityNow: auth,
					positionIds: positionIds
				};
				getPositionList(para).then((res) => {
					this.positions = res.data.data;
				});
			},
			//获取角色职位列表
			getPositions(id) {
				let auth = localStorage.getItem('authority');
				let positionIds = JSON.parse( localStorage.getItem('positionIds'));
				let para = {
					authorityNow: auth,
					positionIds: positionIds,
					role : id
				};
				getPositionList(para).then((res) => {
					this.positions = res.data.data;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row.id };
					removeSysUser(para).then((res) => {
						this.listLoading = false;
						if(res.code == 0) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getUsers();
						} else {
							this.$message({
								message: res.message,
								type: 'error'
							});
						}
					});
				}).catch(() => {});
			},
			//显示查看界面
			handleView: function (index, row) {
				this.getRoles();
				this.viewFormVisible = true;
				this.viewForm = Object.assign({}, row);
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				console.log(row)
				this.getRolesEdit();
				this.editForm = Object.assign({}, row);
				this.getPositionsEdit(row.roleId);
				this.editFormVisible = true;
				
			},
			//获取角色类型列表--编辑--编辑页面弹出时调用
			getRolesEdit() {
				let auth = localStorage.getItem('authority');
				let role = String(localStorage.getItem('role'));
				let para = {
					authority: auth,
					roleIds: role,
				};
				getRoleList(para).then((res) => {
					this.rolesEdit = res.data.data;
				});
			},
			//编辑  获取当前下拉框选中值的value代码值
			getNewCodeEdit(value) {
				this.newRole = value;
				this.editForm.spName = '';
				this.getPositionsEdit(this.newRole);
			},
			//编辑  获取当前下拉框选中值的value代码值---角色类型
			getNewCodeEdit2(value) {
				this.newPositions = value;
			},
			//获取角色职位列表---编辑
			getPositionsEdit(id) {
				let auth = localStorage.getItem('authority');
				let positionIds = JSON.parse( localStorage.getItem('positionIds'));
				let para = {
					authorityNow: auth,
					positionIds: positionIds,
					role : id
				};
				getPositionList(para).then((res) => {
					this.positionsEdit = res.data.data;
					console.log(this.positionsEdit)
				});
			},
			//显示新增界面
			handleAdd: function () {
				this.getRolesAdd();
				this.addFormVisible = true;
				this.addForm = {
					fullname: '',
					username: '',
					password: '',
					role: '',
					position: ''
				};
			},
			//获取角色类型列表--新增--新增页面弹出时调用
			getRolesAdd() {
				let auth = localStorage.getItem('authority');
				let role = String(localStorage.getItem('role'));
				let para = {
					authority: auth,
					roleIds: role,
				};
				getRoleList(para).then((res) => {
					this.rolesAdd = res.data.data;
				});
			},
			//新增  获取当前下拉框选中值的value代码值
			getNewCodeAdd(value) {
				this.newRole = value;
				this.getPositionsAdd(this.newRole);
			},
			//获取角色职位列表---新增
			getPositionsAdd(id) {
				let auth = localStorage.getItem('authority');
				let positionIds = JSON.parse( localStorage.getItem('positionIds'));
				let para = {
					authorityNow: auth,
					positionIds: positionIds,
					role : id
				};
				getPositionList(para).then((res) => {
					this.positionsAdd = res.data.data;
				});
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							let role = this.rolesEdit.find((item)=>{
								return item.name == this.editForm.role
							});
							let position = this.positionsEdit.find((item)=>{
								if(item.name == this.editForm.spName){
									console.log('ss')
								}
								return item.id == this.editForm.spName;
							});
							console.log("p-------"+position);
							let positionId = 0;
							let roleId = 0;
							if(role == null ) {
								roleId = this.editForm.roleId;
								console.log("roleId1--"+roleId);
							} else {
								roleId = role.id;
								console.log("roleId2--"+roleId);
							}
							console.log(this.editForm)
							if(position == null) {
								positionId = String(this.editForm.spId);//this.newPositions;//
								console.log("positionId1--"+positionId);
							} else {
								positionId = String(position.id);
								console.log("positionId2--"+positionId);
							}
							let para = {
								id: this.editForm.id,
								fullname: this.editForm.fullname,
								username: this.editForm.username,
								roleId: roleId,
								positionId: positionId//this.newPositions
							};
							console.log(para);
							this.editLoading = true;
							editSysUser(para).then((res) => {
								this.editLoading = false;
								console.log(res);
								if(res.code == 0) {
									this.$message({
										message: '编辑成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getUsers();
								} else {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = {
								fullname: this.addForm.fullname,
								username: this.addForm.username,
								password: this.addForm.password,
								role: this.addForm.role,
								position: this.addForm.position
							};
							addSysUser(para).then((res) => {
								this.addLoading = false;
								console.log(res);
								if(res.code == 0) {
									this.$message({
										message: '添加成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getUsers();
								} else {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
		},
		mounted() {
			this.getUsers();
			this.getRoles();
			this.getPositionsAll();
		}
	}

</script>

<style scoped>

</style>
